#include "board.h"
#include "rtc.h"
#include <string.h>
#include "ili9341.h"
#include <stdlib.h>
#include <stdio.h>
#include "com.h"
#include "bsp_led.h"

char time_buf[20]= {0};

float temperature;
float vref_value;
float battery_value;

char buf[512]= {0};
uint16_t colors[] = {COLOR_BLUE, COLOR_RED, COLOR_GREEN, COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW,COLOR_WHITE,COLOR_NAVY,COLOR_DARKGREEN,COLOR_DARKCYAN};

char* itoa(int num, char* str, int base) {
    static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    char* cur = str;
    // Handle negative integers
    if (num < 0 && base == 10) {
        *cur++ = '-';
        num = -num;
    }
    // Convert integer to string
    do {
        int digit = num % base;
        *cur++ = digits[digit];
        num /= base;
    } while (num != 0);
    // Add null terminator and reverse string
    *cur-- = '\0';
    while (str < cur) {
        char temp = *str;
        *str++ = *cur;
        *cur-- = temp;
    }
    return str;
}

void show_time();
void show_light(int16_t value);
void show_gb2312(void);

int16_t light_value = 1000;

int main(void) {
    board_init();
    bsp_uart_init();
    //pmu_backup_write_enable();//解锁RTC寄存器
    
    //板载LED灯
    bsp_led_init();
    printf("LED OK\r\n");
    
    //PWM
    pwm_init();

    //LCD初始化
    LCD_IO_Init();
    lcdInit();
    lcdFillRGB(COLOR_BLACK);
    lcdSetOrientation(LCD_ORIENTATION_LANDSCAPE);
    lcdSetTextFont(&Font16);
    lcdSetTextColor(COLOR_WHITE, COLOR_BLACK);
    printf("LCD OK\r\n");

    //随机数初始化
    uint8_t retry = 0;
    while((ERROR == trng_configuration()) && retry < 3) {
        printf("TRNG init fail \r\n");
        printf("TRNG init retry \r\n");
        retry++;
    }
    printf("TRNG OK\r\n");
    
    //SD卡
    sd_fatfs_init();
    printf("SD OK\r\n");
    scan_files("0:");
    
    //ADC
    adc_init();
    
    //DAC
    dac_init();
    
    lcdPrintfGBK(90,200,"\xD6\xC7\xC4\xDC\xB5\xC6\xB9\xE2\xBB\xAD");
    while (1) {
        switch(KEY_Scan(1)){
            case UP_PRES:lcdFillRGB(COLOR_BLACK);show_gb2312();break;
            case DOWN_PRES:
                get_adc_value(&temperature,&vref_value,&battery_value);
                sprintf(buf, "temp:%f,vrefin:%f,battery:%f", temperature,vref_value,battery_value);
                lcdSetCursor(40, 90);
                lcdPrintf("%s",buf);
                break;
            case RIGHT_PRES:
                light_value+=300;
                if(light_value>10000){light_value=10000;};
                show_light(light_value);
                timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,light_value);break;
            case LEFT_PRES:
                light_value-=300;
                if(light_value<0){light_value=0;};
                show_light(light_value);
                timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_TIMER_CH,light_value);break;
            default:break;
        }
        //随机数测试
//        if(SUCCESS == trng_ready_check()) {
//            random_data = trng_get_true_random_data();
//            if(random_data != random_lastdata) {
//                random_lastdata = random_data;
//                lcdSetCursor(5, 50);
//                lcdPrintf("data:0x%08x",random_data);
//            } else {
//                lcdSetCursor(5, 90);
//                lcdPrintf("same data: 0x%08x",random_data);
//            }
//        }
        delay_ms(100);
        show_time();
    }
}

void show_time() {
    RtcShowTime();
    lcdSetCursor(5, 10);
    lcdPrintf("%s",time_buf);
}
void show_light(int16_t value) {
    lcdSetCursor(5, 30);
    lcdPrintf("light:%05d",value);
}
void show_gb2312(void){
    static char temp[2] = {0xb0, 0xa1};
//    for (uint8_t p = 0; p < 10; p++)
//    {
        for(int b = 0;b<15;b++){
            for(int a = 0;a<10;a++){
                temp[1] += 0x01;
                if (temp[1] == 0xff)
                {
                    temp[0] += 0x01;
                    temp[1] = 0xa1;
                }
                if (temp[0] == 0xf7)
                {
                    temp[0] = 0xb0;
                }
                uint16_t cn = (temp[0] << 8) | temp[1];
                sprintf(buf, "GB2312:%x", cn);
                lcdSetCursor(40, 20);
                lcdPrintf("%s",buf);
                lcdPrintfGBK(40+16*a,50+16*b,temp);
            }
        }
//     }
}